//
//  PicListView.m
//  JiaoX
//
//  Created by yinqy on 15/3/2.
//  Copyright (c) 2015年 北京娇羞科技有限公司. All rights reserved.
//  发表新帖里面选择图片的view

#import "PicListView.h"
#import "JTSActionSheet.h"
#define MAXIMAGECOUNT 8

@interface PicListView()
{
    CGFloat itemWidth;
    NSMutableArray *_childArray;
}

@property (nonatomic,weak)UIButton *addPicBtn;
@end

@implementation PicListView


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        itemWidth = (JXScreenW-50)/4;
        self.size = CGSizeMake(JXScreenW, itemWidth+20);
        
        _childArray = [NSMutableArray array];
        
        UIButton *addPicBtn = [[UIButton alloc]initWithFrame:CGRectMake(10, 10, itemWidth, itemWidth)];
        [addPicBtn setBackgroundImage:[UIImage imageNamed:@"bg_add_pic"] forState:UIControlStateNormal];
        [addPicBtn setBackgroundImage:[UIImage imageNamed:@"bg_add_pic_heighlight"] forState:UIControlStateHighlighted];
        [addPicBtn addTarget:self action:@selector(choosePic:) forControlEvents:UIControlEventTouchUpInside];
        self.addPicBtn = addPicBtn;
        [self addSubview:addPicBtn];
    }
    return self;
}

-(void)choosePic:(UIButton *)btn{
    if ([self.delegate respondsToSelector:@selector(picListViewAddPicClick:)]) {
        [self.delegate picListViewAddPicClick:btn];
    }
}

-(NSUInteger)getCanAddPicNum{
    return MAXIMAGECOUNT-_childArray.count;
}

-(NSUInteger)getChildPicNum{
    return _childArray.count;
}
-(void)addImage:(UIImage *)image{
    if (_childArray.count==MAXIMAGECOUNT) return;
    
    //首先获取当前是第几张图片
    NSUInteger currentCount = _childArray.count;
    //再设置此view的高度
    [self setHeight:((currentCount<7?currentCount+1:currentCount)/4+1) *(itemWidth+10)+10];
    
    UIImageView *imageView = [[UIImageView alloc]initWithImage:image];
    [imageView setFrame:[self getFrameWithCurrentCount:currentCount]];
    [imageView setContentMode:UIViewContentModeScaleAspectFill];
    [imageView setClipsToBounds:YES];
    imageView.userInteractionEnabled = YES;
    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showActionSheet:)];
    [imageView addGestureRecognizer:singleTap];
    
    [self addSubview:imageView];
    [_childArray addObject:imageView];
        
    if (currentCount<7){
        [self.addPicBtn setFrame:[self getFrameWithCurrentCount:currentCount+1]];
    }else{
        [self.addPicBtn setHidden:YES];
    }
}

-(void)showActionSheet:(UIGestureRecognizer *)ges{
    
    JTSActionSheetTheme *theme = [JTSActionSheetTheme defaultTheme];
    theme.separatorColor = JXColor(204, 204, 204);
    theme.normalButtonColor = JXThemeColor;
    theme.destructiveButtonColor = [UIColor blackColor];
    theme.titleColor = JXThemeColor;
    theme.cancelButtonColor = [UIColor blackColor];
    
    JTSActionSheetItem *delete = [JTSActionSheetItem itemWithTitle:@"删除" action:^{
        [_childArray removeObject:ges.view];
        [ges.view removeFromSuperview];
        [self resetChildLocation];
    } isDestructive:NO];
    
    JTSActionSheetItem *cancel = [JTSActionSheetItem itemWithTitle:@"取消" action:^{
    } isDestructive:NO];
    
    JTSActionSheet *sheet = [[JTSActionSheet alloc] initWithTheme:theme
                                                            title:nil
                                                      actionItems:@[delete]
                                                       cancelItem:cancel];
    [sheet showInView:[[self superview] superview]];
}


-(CGRect)getFrameWithCurrentCount:(NSUInteger)currentCount{
    NSUInteger row = currentCount/4;//第几行
    NSUInteger col = currentCount%4;//第几列
    
    CGFloat X = col * itemWidth + (col+1)*10;
    CGFloat Y = row * itemWidth + (row+1)*10;
    return CGRectMake(X, Y, itemWidth, itemWidth);
}

-(void)resetChildLocation{
    NSUInteger currentCount = _childArray.count;
    for (int i=0;i<currentCount;i++) {
        UIImageView *imageView = _childArray[i];
        [imageView setFrame:[self getFrameWithCurrentCount:i]];
    }
    [self.addPicBtn setFrame:[self getFrameWithCurrentCount:currentCount]];
    [self.addPicBtn setHidden:NO];
    [self setHeight:((currentCount<7?currentCount:currentCount-1)/4+1) *(itemWidth+10)+10];
}

-(NSArray *)getImageArray{
    NSMutableArray *array = [[NSMutableArray alloc]init];
    for (UIImageView *imageView in _childArray) {
        [array addObject:imageView.image];
    }
    return array;
}

@end
